package cloudUser.config;

import cloudEntity.entity.SmartUserEntity;
import cloudUser.exception.UserIsNullException;
import cloudUser.mapper.SmartSysRolePermissionMapper;
import cloudUser.mapper.SmartUserMapper;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: Smart School
 * @ClassName UserDetailsServiceImpl
 * @description: UserDetailService 实现类，写出用户查询数据出来的逻辑。
 * @author: 陈智博
 * @create: 2025-03-27 20:34
 * @Version 1.0
 **/
@Component
public class CustomerUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SmartUserMapper smartUserMapper;

    @Autowired
    private SmartSysRolePermissionMapper smartSysRolePermissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SmartUserEntity userByName = smartUserMapper.getUserByName(username);

        if(ObjectUtil.isNull(userByName)) {
            throw new UserIsNullException("用户不存在!");
        }

        //将用户 权限 全部都 放入 集合 中
        List<String> permissionList = smartSysRolePermissionMapper.selectRoleAndPermission(userByName.getSysUserId());

        return new CustomerUserDetailsService(userByName,permissionList);
    }

}
